﻿function Stopwatch() {
    var dataStart, dataEnd, result;

    //ctor
    (function () {
        dataStart = 0;
        dataEnd = 0;
        result = 0;
    })();

    function addTotalTime() {
        result = dataEnd - dataStart + result;
    }

    this.Start = function () {
        dataStart = new Date().getTime();
    }
    this.Stop = function () {
        dataEnd = new Date().getTime();

        addTotalTime();
        dataEnd = 0;
        dataStart = 0;
    }
    this.Pause = function () {
        if (dataStart == 0 && dataEnd == 0) {
            this.Start();
        } else {
            this.Stop();
        }
    }
    this.GetTime = function () {
        if (result > 1000) {
            return (result * 1.0 / 1000).toFixed(2) + ' s';
        } else {
            return (result * 1.0).toFixed(2) + ' ms';
        }
    }
}